//
//  LLGradeVC.m
//  LoveLove
//
//  Created by 吴志强 on 2019/5/28.
//  Copyright © 2019 wkf. All rights reserved.
//

#import "LLGradeVC.h"
#import "LLGradeViewController.h"
#import "LLChannelShowView.h"
#import "LLChannelModel.h"
#import "LLSideModel.h"
#import "LLChannelModel.h"
#import "LLSideView.h"

@interface LLGradeVC ()<XYSearchViewDelegate,LLSideViewDelegate>
@property (strong, nonatomic) NSMutableArray *titleArr;
@property (strong, nonatomic) LLSideView *sideView;
@property (strong, nonatomic) NSMutableArray *sideModels;
//y头部标签
@property (nonatomic,strong) NSMutableArray *tagArray;
@property (nonatomic,copy) NSString *id1;
@property (nonatomic,strong) UILabel *labtitle;
@property (nonatomic,strong) LLGradeViewController *grad;

@end

@implementation LLGradeVC

//-(NSMutableArray *)tagArray{
//    if (!_tagArray) {
//
//        LLSideModel *model1 = [LLSideModel new];
//        model1.title = @"热门";
//        model1.iid = @"-1";
//
//        LLSideModel *model2 = [LLSideModel new];
//        model2.title = @"关注";
//        model2.iid = @"0";
//
//        _tagArray = [NSMutableArray arrayWithObjects:model1, model2,nil];
//    }
//    return _tagArray;
//}

//- (NSMutableArray *)titleArr {
//    if (!_titleArr) {
//        NSArray *array = [LLUserDefaults objectForKey:@"user_interest"];
//        NSArray *resultArr = [NSArray yy_modelArrayWithClass:[LLChannelModel class] json:array];
//        self.tagArray = resultArr.mutableCopy;
//        NSMutableArray *arr = [NSMutableArray array];
//        for (LLChannelModel *model in resultArr) {
//            [arr addObject:model.interest_name];
//        }
//
//        LLSideModel *model1 = [LLSideModel new];
//        model1.title = @"热门";
//        model1.iid = @"-1";
//
//        LLSideModel *model2 = [LLSideModel new];
//        model2.title = @"关注";
//        model2.iid = @"0";
//
//        [self.tagArray addObject:model1];
//        [self.tagArray addObject:model2];
//        _titleArr = [[NSMutableArray alloc] initWithArray:@[model1.title,model2.title]];
//        [_titleArr addObjectsFromArray:arr];
//    }
//    return _titleArr;
//}


- (instancetype)init {
    self = [super init];
    if (self) {
        [self initVCSetting];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initNavigationBar];
    [self getSiderList];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.tintColor = UIColor.whiteColor;
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor], NSFontAttributeName:LLRegularFont(18)}];
    self.navigationController.navigationBar.barTintColor = LLColor(218, 13, 13);
}

- (void)initNavigationBar {

    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"reside"] style:UIBarButtonItemStyleDone target:self action:@selector(leftBarButtonItemClick)];

    XYSearchView *searchView = [[XYSearchView alloc] initWithFrame:CGRectMake(0, 0, LLScreenWidth-120, 30) isTap:YES];
    searchView.placeholder = @"请输入搜索内容";
    searchView.delegate = self;
    self.navigationItem.titleView = searchView;

    _labtitle = [[UILabel alloc] initWithFrame:CGRectMake(LLScreenWidth-40, 0, 70, 20)];
    _labtitle.textAlignment = NSTextAlignmentCenter;
    _labtitle.textColor = [UIColor whiteColor];
    _labtitle.textAlignment = NSTextAlignmentRight;
    _labtitle.font = [UIFont systemFontOfSize:15];

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_labtitle];
}

- (void)initVCSetting {
    self.selectIndex = 0;
    self.menuViewStyle = WMMenuViewStyleLine;
    self.titleColorNormal = LLColorRGB(67);
    self.titleSizeNormal = 16;
    self.titleSizeSelected = 16;
    self.titleColorSelected = UIColor.blackColor;
    self.automaticallyCalculatesItemWidths = YES;
    self.progressViewIsNaughty = YES;
    self.progressWidth = 15;
    self.progressHeight = 3;
    self.progressColor = LLColor(225, 65, 52);
    self.progressViewCornerRadius = 1.5;
}

- (NSInteger)numbersOfChildControllersInPageController:(WMPageController *)pageController {
    return self.titleArr.count;
}

- (NSString *)pageController:(WMPageController *)pageController titleAtIndex:(NSInteger)index {
    if (self.titleArr.count > index) {
        return self.titleArr[index];
    }
    return @"";
}

- (UIViewController *)pageController:(WMPageController *)pageController viewControllerAtIndex:(NSInteger)index {

    _grad = LLGradeViewController.new;
    _grad.index = index;
    _grad.pid1 = self.id1;

    if (self.tagArray.count > index) {
        LLSideModel *model = self.tagArray[index];
        _grad.pid2 = model.iid;
    }

    return _grad;
}

-(void)updateView:(NSArray *)tagArray{

    [self.tagArray removeAllObjects];
    [self.titleArr removeAllObjects];
    self.tagArray = nil;
    self.titleArr = nil;

    [self reloadData];
}

- (CGFloat)menuView:(WMMenuView *)menu widthForItemAtIndex:(NSInteger)index {
    CGFloat width = [super menuView:menu widthForItemAtIndex:index];
    return width + 20;
}

- (CGRect)pageController:(WMPageController *)pageController preferredFrameForMenuView:(WMMenuView *)menuView {
        //    CGFloat leftMargin = self.showOnNavigationBar ? 50 : 0;
        //    CGFloat originY = self.showOnNavigationBar ? 0 : CGRectGetMaxY(self.navigationController.navigationBar.frame);

    return CGRectMake(0, 0, self.view.frame.size.width-50, 50);
}

- (CGRect)pageController:(WMPageController *)pageController preferredFrameForContentView:(WMScrollView *)contentView {

    CGFloat originY = CGRectGetMaxY([self pageController:pageController preferredFrameForMenuView:self.menuView]);
    return CGRectMake(0, originY, self.view.frame.size.width, self.view.frame.size.height - originY);
}

#pragma mark - 获取网络请求
- (void) getSiderList
{
    NSDictionary *dic = @{@"edu_id":@"27",@"type_id":@"0"};
    [self showSVProgressHUD];
    [LLHttpTool post:LLHOST headParams:nil params:dic success:^(NSDictionary *result) {

        self.sideModels = [NSMutableArray array];
        NSArray *dataarray = result[@"data"];
        for (int i = 0; i<dataarray.count; i++) {

            NSDictionary *dict = dataarray[i];
            LLSideModel *model = LLSideModel.new;
            model.iid = dict[@"id"];
            model.type = dict[@"pid"];
            model.title = dict[@"name"];
            if (i == 0) {
                model.isSelected = @"1";
            }
            else {
                model.isSelected = @"0";
            }

            [self.sideModels addObject:model];
        }
        self.sideView.models = self.sideModels;

        LLSideModel *model = self.sideModels[0];
        self.id1 = model.iid;
        [self getTopList:model.iid];
        self.labtitle.text = model.title;

    } failure:^(NSString *msg) {
        [SVProgressHUD showErrorWithStatus:msg];
    }];
}

- (void) getTopList:(NSString *)ID
{
    LLSideModel *model1 = [LLSideModel new];
    model1.title = @"热门";
    model1.iid = @"-1";

    LLSideModel *model2 = [LLSideModel new];
    model2.title = @"关注";
    model2.iid = @"0";

    _tagArray = [NSMutableArray arrayWithObjects:model1, model2,nil];
    _titleArr = [NSMutableArray arrayWithObjects:model1.title, model2.title,nil];

    NSDictionary *dic = @{@"edu_id":@"27",@"type_id":ID};
    [self showSVProgressHUD];
    [LLHttpTool post:LLHOST headParams:nil params:dic success:^(NSDictionary *result) {

        NSArray *dataarray = result[@"data"];
        for (int i = 0; i<dataarray.count; i++) {

            NSDictionary *dict = dataarray[i];

            LLSideModel *model = LLSideModel.new;
            model.iid = dict[@"id"];
            model.type = dict[@"pid"];
            model.title = dict[@"name"];
            [self.tagArray addObject:model];
            [self.titleArr addObject:dict[@"name"]];
        }

        [self reloadData];

        [SVProgressHUD dismiss];

    } failure:^(NSString *msg) {
        [SVProgressHUD showErrorWithStatus:msg];
    }];
}


#pragma mark - private method
- (void)leftBarButtonItemClick {
    [self.sideView show];
}

- (void)sideViewDidClickIndex:(NSInteger)index;
{
    LLSideModel *model = self.sideModels[index];
    self.id1 = model.iid;
    [self getTopList:model.iid];

    self.labtitle.text = model.title;
}

#pragma mark - lazy load
- (LLSideView *)sideView {
    if (!_sideView) {
        _sideView = [[LLSideView alloc] initWithFrame:CGRectMake(0, 0, LLScreenWidth, LLScreenHeight)];
        _sideView.delegate = self;
        _sideView.models = self.sideModels;
        [self.tabBarController.view addSubview:_sideView];
    }
    return _sideView;
}

- (void) didSearch:(NSString *)search_text
{
    _grad.search_text = search_text;
}

@end
